package cn.crdeng.controller;

import cn.crdeng.Service.BlogService;
import cn.crdeng.Service.TypeService;
import cn.crdeng.po.Blog;
import cn.crdeng.po.BlogQuery;
import cn.crdeng.po.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 分类
 */

@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String getTypes(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blogQuery, @PathVariable Long id, Model model) {
        List<Type> allType = typeService.findAllType(1000);
        //如果是首页则获取最多的哪个
        if (id == -1) {
            id = allType.get(0).getId();
        }
        blogQuery.setTypeId(id);
        Page<Blog> blogs = blogService.listBlog(pageable, blogQuery);
        model.addAttribute("pageInfo", blogs);
        model.addAttribute("types", allType);
        model.addAttribute("activeId", id);
        return "types";
    }

}
